package org.grayrabbit.cloud.core.biz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ObjectArgument implements BizArgument {
	private static final long serialVersionUID = 6095461937320944088L;
	private String command;
	private Object[] params;
	private Map<String,String> cmdParams = new HashMap<String, String>();
	public ObjectArgument(String command){
		this.command = command;
	}
	public ObjectArgument(String command,String service,String method,Object[] params){
		this(command);
		this.params = params;
		putServiceName(service);
		putMethodName(method);
	}
	public BizArgument rename(String command) {
		this.command = command;
		return this;
	}

	public String getCommand() {
		return this.command ;
	}

	public Object getParams(int pos) {
		return params[pos];
	}

	/**
	 * @return
	 */
	public Object getObject(){
		return getParams(0);
	}
	public void setObject(Object obj){
		this.params = new Object[]{obj};
	}
	public int getParamsCount() {
		if(params!=null)
			return params.length;
		return 0;
	}

	public Object[] getParams() {
		return params;
	}
	
	public BizArgument putParam(String key,String value){
		cmdParams.put(key, value);
		return this;
	}
	private final static String SERVICE_INSTANCE = "bean";
	private final static String METHOD_INSTANCE = "method";
	public BizArgument putServiceName(String service){
		putParam(SERVICE_INSTANCE,service);
		return this;
	}
	public BizArgument putMethodName(String methodName){
		putParam(METHOD_INSTANCE,methodName);
		return this;
	}
	public Map<String, String> getCmdParams() {
		return cmdParams;
	}
	
	@Override
	public Class<?>[] getParamTypes() {
		Class<?>[] types = new Class<?>[getParamsCount()];
		for (int i=0; i<params.length; i++) {
			if(params[i] instanceof List){
				types[i] = List.class;
			}else{
				types[i] = params[i].getClass();
			}
		}
		return types;
	}

}
